"""
@author: wg
@file: homework12_4_3.py
@time: 2023/12/4 17:25
"""

# 3、实现类似ATM存取款功能的一个系统
# 信息固化
#  1、登录
#       登录成功
#       a、存钱 -- 账户金额就增加
#       b、取钱 -- 账户金额就减少，要判断有没有这么多与余额
#       c、转账 给谁转账
#       d、退出
#  2、注册
#       注册 -- 用户名，密码，金额

# 在有可能出现异常的地方，加上异常捕获
# 功能都用函数封装

import json
import os

filename = 'username_passwd_money.txt'

if os.path.isfile(filename):
    with open(filename, "r") as file:
        dt = json.load(file)
else:
    dt = {}

def login(username, passwd):
    if username in dt and dt[username]["passwd"] == passwd:
        return True


def zhuce(username, passwd, money):
    if username not in dt:
        dt[username] = {"passwd": passwd, "balance": money}
        print(f"当前用户有：{dt}")
        return True
    return False

def inputmoney(username, inmoney):
    dt[username]["balance"] += inmoney
    print(f"当前用户余额有：{dt[username]['balance']}")

def outputmoney(username, outmoney):
    dt[username]["balance"] -= outmoney
    print(f"当前用户余额有：{dt[username]['balance']}")

def transfer(username, people, transmoney):
    dt[username]["balance"] -= transmoney
    dt[people]["balance"] += transmoney
    print(f"当前用户余额有：{dt[username]['balance']}")
    print(f"被转账人余额有：{dt[people]['balance']}")

while True:
    print("1、登录")
    print("2、注册")
    print("3、退出")
    num = input("请输入你的选择\n")
    if num == "1":
        for i in range(3):
            username = input("请输入登录用户名\n")
            passwd = input("请输入登录密码\n")
            if login(username, passwd):
                print("登录成功")
                while True:
                    selects = input("a、存钱\nb、取钱\nc、转账\nd、退出\n请输入你的选择\n")
                    if selects == "a":
                        try:
                            inmoney = int(input("请输入存入金额"))
                        except:
                            print("金额格式异常")
                            continue
                        inputmoney(username, inmoney)
                    elif selects == "b":
                        try:
                            outmoney = int(input("请输入取出金额"))
                        except:
                            print("金额格式异常")
                            continue
                        if outmoney > dt[username]["balance"]:
                            print("对不起，您的金额不足")
                            continue
                        outputmoney(username, outmoney)
                    elif selects == "c":
                        people = input("请输入被转账人")
                        if people not in dt:
                            print("不存在该被转账人")
                            continue
                        try:
                            transmoney = int(input("请输入转账金额"))
                        except:
                            print("金额格式异常")
                            continue
                        if transmoney > dt[username]["balance"]:
                            print("对不起，您的金额不足")
                        transfer(username, people, transmoney)
                    elif selects == "d":
                        print("退出")
                        break
                    else:
                        print("输入的选择有误！请重新输入")
                break
            else:
                print(f"登录失败！请重新输入,剩余{2-i}次机会！")
    elif num == "2":
        print("进入用户注册")
        username = input("请输入注册用户名\n")
        passwd = input("请输入注册密码\n")
        try:
            money = int(input("请输入初始金额\n"))
        except:
            print("金额格式异常")
            continue
        if zhuce(username, passwd, money):
            print("注册成功")
        else:
            print("注册失败")
    elif num == "3":
        print("退出")
        break
    else:
        print("输入有误！请重新输入")

with open(filename, "w") as file:
    json.dump(dt, file)
